package com.forever7776.life.core.service.impl.stat;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.forever7776.life.core.common.util.ResultVoUtil;
import com.forever7776.life.core.entity.stat.StatBlog;
import com.forever7776.life.core.mapper.praise.PraiseDetailBlogMapper;
import com.forever7776.life.core.mapper.stat.StatBlogMapper;
import com.forever7776.life.core.service.praise.IPraiseDetailBlogService;
import com.forever7776.life.core.service.stat.IStatBlogService;
import com.forever7776.life.remote.common.enums.StatusEnum;
import com.forever7776.life.remote.common.vo.ResultVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 博客统计表 服务实现类
 * </p>
 *
 * @author kz
 * @since 2018-04-11
 */
@Service
public class StatBlogServiceImpl extends ServiceImpl<StatBlogMapper, StatBlog> implements IStatBlogService {

    @Autowired
    private StatBlogMapper statBlogMapper;
    @Autowired
    private PraiseDetailBlogMapper praiseDetailBlogMapper;
    @Autowired
    private IPraiseDetailBlogService iPraiseDetailBlogService;

    @Override
    public List<StatBlog> selectByBlogIds(List<Integer> thirdIds) {
        return selectList(new EntityWrapper<StatBlog>().in("blog_id", thirdIds).eq("status", StatusEnum.NORMAL.getKey()));
    }

    @Override
    public Map<Integer, StatBlog> selectBlogIdMap(List<Integer> blogIds) {
        List<StatBlog> stats = selectByBlogIds(blogIds);
        if (CollectionUtils.isEmpty(stats)) {
            return null;
        }
        Map<Integer, StatBlog> map = new HashMap<>();
        for (StatBlog s : stats) {
            map.put(s.getBlogId(), s);
        }
        return map;
    }

    /**
     * 给博客添加阅读统计
     *
     * @param blogId
     * @return
     */
    @Override
    public ResultVO addReadStat(Integer blogId) {
        statBlogMapper.addReadStatCount(blogId);
        return ResultVoUtil.getSuccessResult("给博客添加阅读数量成功~");
    }

    /**
     * 给博客添加点赞统计
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO addPraiseStat(String userId, Integer blogId) {
        if (iPraiseDetailBlogService.selectPraiseOneByUserIdAndBlogId(userId, blogId) != null) {
            return ResultVoUtil.getFailResult("该博客已经点赞~");
        }
        statBlogMapper.addPraiseStatCount(blogId);
        iPraiseDetailBlogService.selectOneOrInsert(userId, blogId);
        praiseDetailBlogMapper.updatePraiseBlogStatusPraise(userId, blogId);
        return ResultVoUtil.getSuccessResult("给博客添加点赞成功~");
    }

    /**
     * 给博客取消点赞
     *
     * @param userId
     * @param blogId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO cutPraiseStat(String userId, Integer blogId) {
        if (iPraiseDetailBlogService.selectNotPraiseOneByUserIdAndBlogId(userId, blogId) != null) {
            return ResultVoUtil.getFailResult("该博客已经取消点赞~");
        }
        statBlogMapper.cutPraiseStatCount(blogId);
        iPraiseDetailBlogService.selectOneOrInsert(userId, blogId);
        praiseDetailBlogMapper.updatePraiseBlogStatusNotPraise(userId, blogId);
        return ResultVoUtil.getSuccessResult("给博客取消点赞成功~");
    }
}
